package dao;
import tool.RowMapper;
import tool.BaseDao;
import java.util.List;
import java.sql.SQLException;
import java.sql.ResultSet;
import entity.CustomertbEntity;
import dao.mapper.CustomertbDAOI;

/**
 *@author JiangFan
 */
public class CustomertbDAO extends BaseDao implements CustomertbDAOI {

	public List<CustomertbEntity> selectCustomertb(String userid) {
		String sql="SELECT * FROM customertb WHERE customer_id=?";
		Object[] param = {userid};
		List<CustomertbEntity> list = null;
		try {
			list = executeQuery(sql,param,new CustomertbRowMapper());
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public List<CustomertbEntity> selectCustomertb_email(String userid) {
		String sql="SELECT * FROM customertb WHERE customer_email=?";
		Object[] param = {userid};
		List<CustomertbEntity> list = null;
		try {
			list = executeQuery(sql,param,new CustomertbRowMapper());
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public List<CustomertbEntity> selectcustomer_xiaomi_id(String userid) {
		String sql="SELECT * FROM customertb WHERE customer_xiaomi_id=?";
		Object[] param = {userid};
		List<CustomertbEntity> list = null;
		try {
			list = executeQuery(sql,param,new CustomertbRowMapper());
		} catch (ClassNotFoundException |  SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public int adduserid(String userid) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
		String number=auto_increment();
		String sql="INSERT INTO customertb VALUES (?,null,null,123456789,null,null,null,null,0,?)";
		Object[] param = {userid,number};
		return this.update(sql, param);
	}
	public String auto_increment() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
		String sql="SELECT * FROM customertb ORDER BY customer_xiaomi_id DESC LIMIT 1";
		List<CustomertbEntity> list = null;
		list = executeQuery(sql,null,new CustomertbRowMapper());
		String number=list.get(0).getCustomer_xiaomi_id().toString();
		String num=number.substring(6);
		int aa=Integer.parseInt(num);
		aa+=1;
		num=String.valueOf(aa);
		number=number.substring(0,6)+num;
		return number;

	}
	public int adduser(String userid,String username,String userpassword,String usermail ) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
		String number=auto_increment();
		String sql="INSERT INTO customertb VALUES (?,?,null,?,?,null,null,null,0,?)";
		Object[] param = {userid,username,userpassword,usermail,number};
		return this.update(sql, param);
	}

}
class CustomertbRowMapper implements RowMapper<CustomertbEntity>{
	@Override
	public CustomertbEntity rowmap(ResultSet set) throws  SQLException{
		return new CustomertbEntity(
				set.getString("customer_id"),
				set.getString("customer_name"),
				set.getString("customer_picture"),
				set.getString("customer_password"),
				set.getString("customer_email"),
				set.getString("customer_sex"),
				set.getString("customer_problem"),
				set.getString("customer_answer"),
				set.getDouble("customer_balance"),
				set.getString("customer_xiaomi_id")
		);
	}

}